package com.chg.mymvc.dao;

import com.chg.mymvc.entity.Item;
import com.fJdbcUtil.util.FJdbc;

import java.util.HashMap;
import java.util.List;

public class ItemDaoImpl implements ItemDao{
    @Override
    public void delItem(int id) {
        String sql="delete from item where id=?";
        Object[] objs = new Object[1];
        objs[0]=id;
        FJdbc.update(sql,objs);
    }

    @Override
    public void updateItem(Item item) {
        String sql="update item set perm=? where id=?";
        Object[] objs = new Object[2];
        objs[0]=item.getPerm();
        objs[1]=item.getId();
        FJdbc.update(sql,objs);
    }

    @Override
    public List<Item> queryAll(int page, int pagesize) {
        String sql="select id,perm,sequence from item order by sequence asc limit ?,?";
        Object[] params=new Object[]{(page-1)*pagesize,pagesize};
        return FJdbc.queryList(sql,Item.class,params);
    }
    @Override
    public List<Item> getAllItem() {
        String sql="select id,perm from item";
//        Object[] params=new Object[]{2,5};
        return FJdbc.queryList(sql,Item.class,null);
    }
    @Override
    public Item getItemByName(String name) {
        String sql="select id,perm from item where name=?";
        Object[] params=new Object[]{name};
        return FJdbc.queryOne(sql,Item.class,params);
    }

    @Override
    public int addItem(Item item) {
        String sql="insert into item (sequence,perm) values(?,?)";
        Object[] objs = new Object[2];
        objs[0]=item.getSequence();
        objs[1]=item.getPerm();
        FJdbc.update(sql, objs);
        return 1;
    }

    @Override
    public Item selecItemtById(int id) {
        String sql="select id,perm from item where id=?";
        Object[] objs = new Object[1];
        objs[0]=id;
        return FJdbc.queryOne(sql, Item.class,objs);
    }

    @Override
    public int allcount() {
        String sql="select count(id) from item";
        HashMap<String,Object> map = FJdbc.queryOne(sql, HashMap.class);
        long count = (long)map.get("count(id)");
        return new Long(count).intValue();
    }

    @Override
    public Item getNewsidByItemId(String name) {
        String sql="select id from item where perm like ?";
        Object[] params=new Object[]{"%"+name+"%"};
        return FJdbc.queryOne(sql,Item.class,params);
    }


}
